﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.vehicle.apply
{
    public partial class edit : ModulePage
    {

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["refer"] != null) ? Request.QueryString["refer"].ToString() : "vehicle_wait.aspx";
                this.ViewState["Refer"] = str;
                int usageId = (Request.QueryString["usage_id"] != null) ? int.Parse(Request.QueryString["usage_id"]) : 0;
                this.ViewState["UsageId"] = usageId;
                DataTable table = new VehicleUsageDao().SelectByUsageId(base.Conn, usageId);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此车辆申请", "new.aspx");
                    return;
                }
                VehicleDao dao2 = new VehicleDao();
                DataTable table2 = dao2.SelectAll(base.Conn);
                this.ddlVehicleId.DataSource = table2;
                this.ddlVehicleId.DataValueField = "VehicleId";
                this.ddlVehicleId.DataTextField = "Number";
                this.ddlVehicleId.DataBind();
                this.ddlDriver.DataSource = table2;
                this.ddlDriver.DataValueField = "Driver";
                this.ddlDriver.DataTextField = "Driver";
                this.ddlDriver.DataBind();
                DataTable table3 = dao2.SelectOperator(base.Conn);
                this.ddlOperator.DataSource = table3;
                this.ddlOperator.DataValueField = "UserId";
                this.ddlOperator.DataTextField = "UserName";
                this.ddlOperator.DataBind();
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.Vehicle);
                this.ckbMobileRemind.Visible = SmsDao.IsMobile(SmsDao.SmsType.Vehicle);
                if (this.ddlVehicleId.Items.FindByValue(table.Rows[0]["VehicleId"].ToString()) != null)
                {
                    this.ddlVehicleId.Items.FindByValue(table.Rows[0]["VehicleId"].ToString()).Selected = true;
                }
                if (this.ddlDriver.Items.FindByValue(table.Rows[0]["Driver"].ToString()) != null)
                {
                    this.ddlDriver.Items.FindByValue(table.Rows[0]["Driver"].ToString()).Selected = true;
                }
                this.txtPerson.Text = (table.Rows[0]["Person"] != DBNull.Value) ? table.Rows[0]["Person"].ToString() : "";
                this.ToId.Value = (table.Rows[0]["DeptId"] != DBNull.Value) ? table.Rows[0]["DeptId"].ToString() : "";
                this.ToName.Text = (table.Rows[0]["DeptName"] != DBNull.Value) ? table.Rows[0]["DeptName"].ToString() : "";
                this.txtStartTime.Text = table.Rows[0]["StartTime"].ToString();
                this.txtEndTime.Text = table.Rows[0]["EndTime"].ToString();
                this.txtDestination.Text = (table.Rows[0]["Destination"] != DBNull.Value) ? table.Rows[0]["Destination"].ToString() : "";
                this.txtMileage.Text = (table.Rows[0]["Mileage"] != DBNull.Value) ? table.Rows[0]["Mileage"].ToString() : "";
                this.txtProposer.Text = (table.Rows[0]["ProposerName"] != DBNull.Value) ? table.Rows[0]["ProposerName"].ToString() : "";
                if (this.ddlOperator.Items.FindByValue(table.Rows[0]["Operator"].ToString()) != null)
                {
                    this.ddlOperator.Items.FindByValue(table.Rows[0]["Operator"].ToString()).Selected = true;
                }
                this.txtReason.Text = (table.Rows[0]["Reason"] != DBNull.Value) ? table.Rows[0]["Reason"].ToString() : "";
                this.txtRemark.Text = (table.Rows[0]["Remark"] != DBNull.Value) ? table.Rows[0]["Remark"].ToString() : "";
                this.ckbSmsRemind.Checked = Convert.ToBoolean(table.Rows[0]["SmsRemind"].ToString());
                this.ckbMobileRemind.Checked = Convert.ToBoolean(table.Rows[0]["MobileRemind"].ToString());
                this.ddlVehicleId.Attributes.Add("onchange", "showDetail();");
            }
            if (this.Page.IsPostBack)
            {
                VehicleUsageDao dao3 = new VehicleUsageDao();
                VehicleUsageData data = new VehicleUsageData();
                data.UsageId = Convert.ToInt32(this.ViewState["UsageId"].ToString());
                data.VehicleId = Convert.ToInt32(this.ddlVehicleId.SelectedValue);
                if (this.ddlDriver.SelectedValue != string.Empty)
                {
                    data.Driver = this.ddlDriver.SelectedValue;
                }
                if (this.txtPerson.Text != string.Empty)
                {
                    data.Person = this.txtPerson.Text;
                }
                if (this.ToId.Value != string.Empty)
                {
                    data.DeptId = Convert.ToInt32(this.ToId.Value);
                }
                data.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                data.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                if (this.txtDestination.Text != string.Empty)
                {
                    data.Destination = this.txtDestination.Text;
                }
                if (this.txtMileage.Text != string.Empty)
                {
                    data.Mileage = Convert.ToDecimal(this.txtMileage.Text);
                }
                data.Proposer = base.User.Identity.Name;
                data.Operator = this.ddlOperator.SelectedValue;
                if (this.txtReason.Text != string.Empty)
                {
                    data.Reason = this.txtReason.Text;
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    data.Remark = this.txtRemark.Text;
                }
                data.SmsRemind = this.ckbSmsRemind.Checked;
                data.MobileRemind = this.ckbMobileRemind.Checked;
                dao3.Update(base.Conn, data);
                if ((this.ckbSms.Checked || this.ckbMobile.Checked) && (this.ddlOperator.SelectedValue != string.Empty))
                {
                    SmsDao dao4 = new SmsDao();
                    SmsData data2 = new SmsData();
                    data2.Type = 9;
                    data2.FromId = base.User.Identity.Name;
                    data2.ToId = this.ddlOperator.SelectedValue;
                    data2.Content = "您有新的车辆申请需要审批。";
                    data2.SendTime = DateTime.Now;
                    dao4.BizSend(base.Conn, data2, this.ckbSms.Checked, this.ckbMobile.Checked);
                }
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPrompt("车辆申请修改成功！", this.ViewState["Refer"].ToString());
            }
        } 

    }
}
